﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Google.GData.Spreadsheets;

namespace Sriram.GoogleProviders
{
    /// <summary>
    /// Summary description for ProviderUtil
    /// </summary>
    internal static class ProviderUtil
    {
        //
        // A helper function to retrieve config values from the configuration file.
        //

        public static string GetConfigValue(string configValue, string defaultValue)
        {
            if (String.IsNullOrEmpty(configValue))
                return defaultValue;

            return configValue;
        }

        /// <summary>
        /// Helper method to convert ListEntry(a row from spreadsheet) to MembershipUser object
        /// </summary>
        /// <param name="firstRow">ListEntry returned from Google</param>
        /// <returns>MembershipUser</returns>

        public static MembershipUser GetUserFromSpreadsheet(string providerName, ListEntry firstRow)
        {
            object providerUserKey = firstRow.Elements[0].Value;
            string username = firstRow.Elements[1].Value;
            string email = firstRow.Elements[3].Value;
            string passwordQuestion = "";
            if (firstRow.Elements[4].Value != "")
                passwordQuestion = firstRow.Elements[4].Value;

            string comment = "";
            //if (reader.GetValue(4) != DBNull.Value)
            //    comment = reader.GetString(4);

            bool isApproved = Convert.ToBoolean(firstRow.Elements[6].Value);
            bool isLockedOut = Convert.ToBoolean(firstRow.Elements[12].Value);
            DateTime creationDate = Convert.ToDateTime(firstRow.Elements[8].Value);
            DateTime lastLoginDate = new DateTime();
            if (firstRow.Elements[18].Value != "")
                lastLoginDate = Convert.ToDateTime(firstRow.Elements[18].Value);

            DateTime lastActivityDate = Convert.ToDateTime(firstRow.Elements[10].Value);
            DateTime lastPasswordChangedDate = Convert.ToDateTime(firstRow.Elements[9].Value);

            DateTime lastLockedOutDate = new DateTime();
            if (firstRow.Elements[13].Value != "")
                lastLockedOutDate = Convert.ToDateTime(firstRow.Elements[13].Value);

            MembershipUser u = new MembershipUser(providerName,
                                                  username,
                                                  providerUserKey,
                                                  email,
                                                  passwordQuestion,
                                                  comment,
                                                  isApproved,
                                                  isLockedOut,
                                                  creationDate,
                                                  lastLoginDate,
                                                  lastActivityDate,
                                                  lastPasswordChangedDate,
                                                  lastLockedOutDate);

            return u;
        }

         //
        // CheckPassword
        //   Compares password values based on the MembershipPasswordFormat. Only clear text format supported.
        //

        public static bool CheckPassword(string password, string dbpassword)
        {
            string pass1 = password;
            string pass2 = dbpassword;

            //Implement later
            //switch (PasswordFormat)
            //{
            //    case MembershipPasswordFormat.Encrypted:
            //        pass2 = UnEncodePassword(dbpassword);
            //        break;
            //    case MembershipPasswordFormat.Hashed:
            //        pass1 = EncodePassword(password);
            //        break;
            //    default:
            //        break;
            //}

            if (pass1 == pass2)
            {
                return true;
            }

            return false;
        }

    }
    
}
